package com.gopay.scheduler.job;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.alarm.CheckNotfiyOrderAlarmService;

/**
 * 检测00100订单，交易成功，notfiy表中无通知的报警
 * zhg.zhang
 * 2016年11月25日 
 * www.gopay.com.cn Inc.All rights reserved.
 */
public class CheckNotfiyOrderAlarmJob extends GopayBaseJob{

	private Logger logger = LoggerFactory.getLogger(CheckNotfiyOrderAlarmJob.class);
	
	@Resource(name="checkNotfiyOrderAlarmService")
	private CheckNotfiyOrderAlarmService checkNotfiyOrderAlarmService;
	
	
	@Value("#{webProperties['checkOrderAlarm.allowMaxCounts']}")
	private String ALLOW_MAX_COUNT;
	
	@Value("#{webProperties['checkOrderAlarm.startMinute']}")
	private String START_MINUTE;
	
	@Value("#{webProperties['checkOrderAlarm.endminute']}")
	private String END_MINUTE;
	
	@Value("#{webProperties['checkOrderAlarm.phones']}")
	private  String PHONES;
	
	@Override
	protected void doJobInternal() {
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		String startDate = getTimeByMinute(Integer
				.valueOf(START_MINUTE == null ? "-65" : START_MINUTE));
		String endDate = getTimeByMinute(Integer
				.valueOf(END_MINUTE == null ? "-5" : END_MINUTE));

		map.put("startDate", startDate);
		map.put("endDate", endDate);

		logger.info("CheckNotfiyOrderAlarmJob-doJobInternal-startDate:" + startDate + ",endDate:" + endDate);

		int count = checkNotfiyOrderAlarmService.getNotNotfiyOrderCounts(map);

		if(count>0){
			checkNotfiyOrderAlarmService.insertNotfiyOrder(map);
		}
		
		if (Integer.valueOf(ALLOW_MAX_COUNT == null ? "20" : ALLOW_MAX_COUNT) < count) {
			logger.info("CheckNotfiyOrderAlarmJob-doJobInternal-SMS ALARM count=" + count);
			checkNotfiyOrderAlarmService.sendMassageAlarm(count, PHONES);
		}
		
	}
	
	
/*	//拆分手机号
	private static String[] getPhone(String phoneStr){
		String[] phones=phoneStr.split(",");
		if(phones!=null&&phones.length>0){
			
			return phones;
		}
		return null;
	}*/
	//获取当前时间的前minute分钟
	private static String getTimeByMinute(int minute) {

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.MINUTE, minute);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
	}
	
}
